package com.code.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.code.pojo.Admin;
import com.code.pojo.Role;
import com.code.service.AdminService;
import com.code.service.RoleService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

public class UserDetailsServiceImpl implements UserDetailsService {
    @Reference
    AdminService adminService;

    @Reference
    RoleService roleService;

//    @Autowired
//    BCryptPasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Admin admin = adminService.findUserByUserName(username);
        Role role = adminService.findRoleByAdmin(admin.getRid());
//        String password = "{noop}"+admin.getPassword();
//        String password =user.getPassword();
//        String encode = passwordEncoder.encode(password);
        List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
        list.add(new SimpleGrantedAuthority(role.getRoleName()));
//        Roles roles = rolesService.findRoleByRoleId(user.getRoleId());
//            list.add(new SimpleGrantedAuthority("ROLE_"+roles.getRoleName()));
        return new User(username,admin.getPassword(),list);
    }
}

